package com.after.saas.common.utils;

import cn.hutool.core.collection.CollUtil;
import com.after.saas.common.constants.Constants;
import com.after.saas.common.model.vo.UserVO;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 登录工具类
 *
 * @date 2021-05-07
 * @Created by hjchen
 */
public class LoginUtil {

    /**
     * 登录过期时间
     */
    private static final long LOGIN_EXPIRE_TIME = 30 * 24 * 60 * 60L;


    /**
     * 生成登录redis 缓存 JWT 的 key
     *
     * @param userId
     * @return
     */
    public static String getRedisCacheLoginJWTKey(String userId) {
        return formatKey(Constants.APP_NAME, Constants.SUB_NAME_LOGIN_JWT, userId);
    }

    /**
     * 生成登录redis 缓存 用户对象 的 key
     *
     * @param userId
     * @return
     */
    public static String getRedisCacheLoginUserKey(String userId) {
        return formatKey(Constants.APP_NAME, Constants.SUB_NAME_LOGIN_USER, userId);
    }

    /**
     * 生成登录redis 缓存 用户权限 的 key
     *
     * @param userId
     * @return
     */
    public static String getRedisCacheUserPermissionKey(String userId) {
        return formatKey(Constants.APP_NAME, Constants.SUB_NAME_USER_PERMISSIONS, userId);
    }

    /**
     * 生成登录redis 缓存 用户角色 的 key
     *
     * @param userId
     * @return
     */
    public static String getRedisCacheUserRoleKey(String userId) {
        return formatKey(Constants.APP_NAME, Constants.SUB_NAME_USER_ROLES, userId);
    }


    /**
     * 缓存登录token
     *
     * @param redisTemplate
     * @param loginToken
     * @param userId
     */
    public boolean cacheLoginToken(RedisTemplate redisTemplate, String loginToken, String userId) {
        return RedisUtil.storeObject(redisTemplate, getRedisCacheLoginJWTKey(userId), loginToken, LOGIN_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 缓存登录用户相关信息
     *
     * @param redisTemplate
     * @param userVO
     * @param userId
     */
    public static boolean cacheUserInfo(RedisTemplate redisTemplate, UserVO userVO, String userId) {
        return RedisUtil.storeObject(redisTemplate, getRedisCacheLoginUserKey(userId), userVO, LOGIN_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 缓存登录用户权限相关信息
     *
     * @param redisTemplate
     * @param permissionList
     * @param userId
     * @return
     */
    public static boolean cacheUserPermissions(RedisTemplate redisTemplate, List<String> permissionList, String userId) {
        return RedisUtil.listSet(redisTemplate, getRedisCacheUserPermissionKey(userId), permissionList, LOGIN_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 缓存登录用户权限相关信息
     *
     * @param redisTemplate
     * @param roleList
     * @param userId
     * @return
     */
    public static boolean cacheUserRoles(RedisTemplate redisTemplate, List<String> roleList, String userId) {
        return RedisUtil.listSet(redisTemplate, getRedisCacheUserRoleKey(userId), roleList, LOGIN_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 用户登出
     *
     * @param redisTemplate
     * @param userId
     */
    public static void userLogout(RedisTemplate redisTemplate, String userId) {
        RedisUtil.deleteKeys(redisTemplate, getRedisCacheLoginJWTKey(userId),
                getRedisCacheLoginUserKey(userId), getRedisCacheUserRoleKey(userId),
                getRedisCacheUserPermissionKey(userId));
    }


    /**
     * 用户登录来源 缓存key
     *
     * @param userId
     * @return
     */
    public static String getUserLoginSourceKey(String userId) {
        return formatKey(Constants.APP_NAME, Constants.USER_LOGIN_SOURCE, userId);

    }

    /**
     * 缓存用户登录来源
     *
     * @param redisTemplate
     * @param userId        用户id
     * @param loginSource   登录来源 1：PC登录 2：移动端登录
     * @return
     */
    public static boolean setMemberLoginSource(RedisTemplate redisTemplate, String userId, Integer loginSource) {
        return RedisUtil.storeObject(redisTemplate, getUserLoginSourceKey(userId), loginSource);
    }

    /**
     * 获取用户登录来源
     *
     * @param redisTemplate
     * @param userId
     * @return
     */
    public static Object getUserLoginSource(RedisTemplate redisTemplate, String userId) {
        return RedisUtil.getObject(redisTemplate, getUserLoginSourceKey(userId));
    }

    public static void refreshUserInfo(RedisTemplate redisTemplate, UserVO dbUser) {
        String userid = dbUser.getId();
        LoginUtil.cacheUserInfo(redisTemplate, dbUser, userid);
        List<String> permissions = dbUser.getPermissions();
        List<String> roles = dbUser.getRoles();
        if (!CollUtil.isEmpty(permissions)) cacheUserPermissions(redisTemplate, permissions, userid);
        if (!CollUtil.isEmpty(roles)) cacheUserRoles(redisTemplate, roles, userid);
    }

    public static String formatKey(String s1, String s2, String unique) {
        return String.format("%s:%s:%s", s1, s2, unique);
    }

}
